package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerCollection;
import com.woniu.service.FlowerCollectionService;
import com.woniu.util.R;
import com.woniu.vo.CollectionVoCZT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("flowerCollection")
@Api(value = "收藏接口", tags = "收藏功能")
public class FlowerCollectionController {
    @Resource
    private FlowerCollectionService fcs;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @ApiOperation("核查用户是否收藏该鲜花商品")
    @GetMapping("checkIsCollect")
    public R checkIsCollect(int accountId, int flowerId) {
        FlowerCollection flowerCollection = fcs.checkIsCollect(accountId, flowerId);
        if (flowerCollection != null) {
            return R.success("已收藏");
        } else {
            return R.error("未收藏");
        }
    }

    @ApiOperation("用户添加收藏")
    @PostMapping("insertCollection")
    public R insertCollection(int accountId, int flowerId) {
        int i = fcs.insertCollection(accountId, flowerId);
        if (i > 0) {
            Set<String> set = redisTemplate.keys("collectionSelect*");
            if (set == null || set.size() == 0) {
                return R.success("收藏成功");
            }
            redisTemplate.delete(set);
            return R.success("收藏成功");
        } else {
            return R.error("收藏失败");
        }
    }

    @ApiOperation("用户取消收藏")
    @PostMapping("deleteCollection")
    public R deleteCollection(int accountId, int flowerId) {
        int i = fcs.deleteCollection(accountId, flowerId);
        if (i > 0) {
            Set<String> set = redisTemplate.keys("collectionSelect*");
            if (set == null || set.size() == 0) {
                return R.success("取消收藏成功");
            }
            redisTemplate.delete(set);
            return R.success("取消收藏成功");
        } else {
            return R.error("取消收藏失败");
        }
    }

    @ApiOperation("查看用户的所有收藏")
    @GetMapping("selectAllCollectionByAccountId")
    public R selectAllCollectionByAccountId(int accountId, int pageNum, int pageSize) {
        Object temp = redisTemplate.opsForValue().get("collectionSelect:" + accountId + pageNum + pageSize);
        if (temp != null) {
            return R.build(200, "从redis中查询成功", temp);
        } else {
            PageInfo<CollectionVoCZT> pageInfo = fcs.selectAllCollectionByAccountId(accountId, pageNum, pageSize);
            redisTemplate.opsForValue().set("collectionSelect:" + accountId + pageNum + pageSize, pageInfo);
            redisTemplate.expire("collectionSelect:" + accountId + pageNum + pageSize, 30, TimeUnit.MINUTES);
            return R.build(200, "查询成功", pageInfo);
        }
    }
}
